﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VehicleManagement.Models;
using VehicleManagement.ControllersServices;
using System.Collections;

namespace VehicleManagement.Controllers
{
    //[CustomerAuthorize(Roles = "管理员,负责人,申请人,财务人员,司机")]
    public class MessageController : Controller
    {
        MessageService messageService = new MessageService();

        //
        // GET: /Message/
        /// <summary>
        /// 管理员消息
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            if (Session["UserID"] == null)
                return View("/Home/Login");
            Hashtable ht = new Hashtable();
            ht.Add("receiverID", Session["UserID"].ToString());
            ht.Add("receiverRole", Session["UserRole"].ToString());

            IList<Message> lstModel = messageService.GetMyMessage(ht);
            return View(lstModel);
        }

        /// <summary>
        /// 统计未读消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public int CountNewMessage()
        {
            Hashtable ht = new Hashtable();
            if (Session["UserID"] == null || Session["UserRole"] == null)
            {
                return 0;
            }
            ht.Add("receiverID", Session["UserID"].ToString());
            ht.Add("receiverRole", Session["UserRole"].ToString());
            int CountNewMessage = messageService.CountNewMessage(ht);

            return CountNewMessage;
        }

        /// <summary>
        /// 改变消息是否读过的状态
        /// </summary>
        /// <param name="id">消息ID</param>
        /// <returns></returns>
        [HttpPost]
        public int ChangeMessageState(int id)
        {
            return messageService.ChangeMessageState(id);
        }

        //
        // GET: /Message/DriverManageMessage
        /// <summary>
        /// 负责人信息
        /// </summary>
        /// <returns></returns>
        public ViewResult DriverManageMessage()
        {
            if (Session["UserID"] == null)
                return View("/Home/Login");
            Hashtable ht = new Hashtable();
            ht.Add("receiverID", Session["UserID"].ToString());
            ht.Add("receiverRole", Session["UserRole"].ToString());

            IList<Message> lstModel = messageService.GetMyMessage(ht);
            return View(lstModel);
        }

        //
        // GET: /Message/ApplicantMessage
        /// <summary>
        /// 申请人消息
        /// </summary>
        /// <returns></returns>
        public ViewResult ApplicantMessage()
        {
            if (Session["UserID"] == null)
                return View("/Home/Login");
            Hashtable ht = new Hashtable();
            ht.Add("receiverID", Session["UserID"].ToString());
            ht.Add("receiverRole", Session["UserRole"].ToString());

            IList<Message> lstModel = messageService.GetMyMessage(ht);
            return View(lstModel);
        }

        //
        // GET: /Message/FinanManMessage
        /// <summary>
        /// 财务人员消息
        /// </summary>
        /// <returns></returns>
        public ViewResult FinanManMessage()
        {
            if (Session["UserID"] == null)
                return View("/Home/Login");
            Hashtable ht = new Hashtable();
            ht.Add("receiverID", Session["UserID"].ToString());
            ht.Add("receiverRole", Session["UserRole"].ToString());

            IList<Message> lstModel = messageService.GetMyMessage(ht);
            return View(lstModel);
        }

        //
        // GET: /Message/Details/5
        /// <summary>
        /// 消息详细视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ViewResult Details(int id)
        {
            if (Session["UserID"] == null)
                return View("/Home/Login");
            Hashtable ht = new Hashtable();
            ht.Add("receiverID", Session["UserID"].ToString());
            ht.Add("receiverRole", Session["UserRole"].ToString());
            ht.Add("messageID", id);

            IList<Message> lstModel = messageService.GetMessageByID(ht);
            Message message = lstModel.Where(u => u.MessageID == id).First();
            return View(message);
        }

        //
        // GET: /Message/Create
        /// <summary>
        /// 发送新消息
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Message/Create

        [HttpPost]
        public ActionResult Create(Message message)
        {
            if (Session["CompanyName"] == null)
            {
                return View("../Home/Login");
            }
            message.CompanyName = Session["CompanyName"].ToString();

            if (ModelState.IsValid)
            {
                object obj = messageService.InsertMessage(message);
                return RedirectToAction("Index");
            }

            return View(message);
        }

        //
        // POST: /Message/DeleteMessage/5

        /// <summary>
        /// 使用AJAX删除一个消息
        /// </summary>
        /// <param name="id">消息ID</param>
        /// <returns></returns>
        [HttpPost, ActionName("DeleteMessage")]
        public ActionResult DeleteConfirmed(int id)
        {
            if (Request.IsAjaxRequest())
            {
                int i = messageService.DeleteMessage(id);
                if (i > 0)
                {
                    return Json(new { Result = "true", Message = "删除成功" });
                }
                else
                {
                    return Json(new { Result = "false", Message = "删除失败" });
                }
            }
            else
            {
                return Json(new { Result = "false", Message = "该方法只能通过AJAX方式调用" });
            }
        }

        ////
        //// GET: /Message/Edit/5

        //public ActionResult Edit(int id)
        //{

        //    IList<Message> lstModel = messageService.GetMessageByID(id);
        //    Message message = lstModel.Where(u => u.MessageID == id).First();
        //    return View(message);
        //}

        ////
        //// POST: /Message/Edit/5

        //[HttpPost]
        //public ActionResult Edit(Message message)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        messageService.UpdateMessage(message);
        //        return RedirectToAction("Index");
        //    }
        //    return View(message);
        //}

    }
}
